#include "../include/time.h"
#include "../include/io.h"
#include "../include/kasm.h"

/* Fuente: http://forum.osdev.org/viewtopic.php?f=1&t=17354&start=0&st=0&sk=t&sd=a */
static void fetch_time(time_t *ret);


void
fetch_time(time_t *ret)
{
    _Cli();
    ret->sec = BCD2BIN(readCMOS(0x0));
    ret->min = BCD2BIN(readCMOS(0x2));
    ret->hour = BCD2BIN(readCMOS(0x4));
    ret->day = BCD2BIN(readCMOS(0x7));
    ret->month = BCD2BIN(readCMOS(0x8));
    ret->year = BCD2BIN(readCMOS(0x9));
    _Sti();
}

void
date(void)
{
    time_t time;
    char *month[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
                     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
    fetch_time(&time);

    /*Imprimo algo parecido al date de linux:
    Sat Nov  8 15:28:53 ARST 2008*/
    printf("%s %d %d:%d:%d GMT-0 %d\n", month[time.month-1],
            time.day, time.hour, time.min, time.sec, time.year+2000);
    return;
}

int
getYear(void)
{
    time_t time;
    fetch_time(&time);

    return time.year+2000;
}

int
getDay(void)
{
    time_t time;
    fetch_time(&time);

    return time.day;
}

int
getSec(void)
{
    time_t time;
    fetch_time(&time);

    return time.sec;
}

int
getMin(void)
{
    time_t time;
    fetch_time(&time);

    return time.min;
}

int
getHour(void)
{
    time_t time;
    fetch_time(&time);

    return time.hour;
}

